local function OnTimerDone(inst, data)
    if data.name == "buffover" then
        inst.components.debuff:Stop()
    end
end

local function OnExtended(inst, target)
    inst.components.timer:StopTimer("buffover")
    inst.components.timer:StartTimer("buffover", 10)
end

local function OnTick(inst, target)
    if target.components.temperature ~= nil and not target.components.health:IsDead() and not target:HasTag("playerghost") then
        local current = target.components.temperature:GetCurrent()
        if current > 40 then                                 -- 当前体温高于40度时
            target.components.temperature:SetTemperature(40) -- 将温度重新设置为40
        end
    end
end

local function OnAttached(inst, target)
    inst.entity:SetParent(target.entity)
    inst.Transform:SetPosition(0, 0, 0) -- 在装载的情况下
    inst.task = inst:DoPeriodicTask(2, OnTick, nil, target)
    inst:ListenForEvent("death", function()
        inst.components.debuff:Stop()
    end, target)
    target:AddTag("notarget")
    if target.bboycloudsover then
        target.bboycloudsover:set(true)
    end
end

local function OnDetached(inst, target)
    target:RemoveTag("notarget")
    if target.bboycloudsover then
        target.bboycloudsover:set(false)
    end
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    if not TheWorld.ismastersim then
        -- 不适合客户端
        inst:DoTaskInTime(0, inst.Remove)

        return inst
    end

    inst.entity:AddTransform()
    inst.entity:Hide()
    inst.persists = false

    inst:AddTag("CLASSIFIED")

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff:SetDetachedFn(OnDetached)
    inst.components.debuff:SetExtendedFn(OnExtended)

    inst:AddComponent("timer")
    inst.components.timer:StartTimer("buffover", 10)
    inst:ListenForEvent("timerdone", OnTimerDone)

    return inst
end

return Prefab("bboy_nether_buff", fn)
